package org.example.meetingsystem.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.example.meetingsystem.entity.Reservation;

import java.util.*;

@Mapper
public interface ReservationMapper extends BaseMapper<Reservation> {

    /**
     * 查询用户的预订记录（包含关联信息）
     */
    @Select("SELECT r.*, u.name as customer_name, u.company_name, " +
            "mr.name as room_name, mr.type as room_type " +
            "FROM reservations r " +
            "LEFT JOIN users u ON r.customer_id = u.id " +
            "LEFT JOIN meeting_rooms mr ON r.room_id = mr.id " +
            "WHERE r.customer_id = #{customerId} " +
            "ORDER BY r.created_at DESC")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "customer_id", property = "customerId"),
            @Result(column = "room_id", property = "roomId"),
            @Result(column = "customer_name", property = "customer.name"),
            @Result(column = "company_name", property = "customer.companyName"),
            @Result(column = "room_name", property = "room.name"),
            @Result(column = "room_type", property = "room.type")
    })
    List<Reservation> selectByCustomerId(@Param("customerId") Long customerId);

    /**
     * 查询过期的锁定预订
     */
    @Select("SELECT * FROM reservations WHERE status = 'LOCKED' AND lock_expires_at < NOW()")
    List<Reservation> selectExpiredLocks();

    /**
     * 查询今日使用的会议室
     */
    @Select("SELECT r.*, mr.name as room_name FROM reservations r " +
            "LEFT JOIN meeting_rooms mr ON r.room_id = mr.id " +
            "WHERE DATE(r.start_time) = CURDATE() " +
            "AND r.status IN ('CONFIRMED', 'IN_USE') " +
            "ORDER BY r.start_time")
    List<Reservation> selectTodayReservations();

    /**
     * 查询用户可以取消的预订记录（不存在取消申请记录的预订）
     */
    @Select("SELECT r.*, u.name as customer_name, u.company_name, " +
            "mr.name as room_name, mr.type as room_type " +
            "FROM reservations r " +
            "LEFT JOIN users u ON r.customer_id = u.id " +
            "LEFT JOIN meeting_rooms mr ON r.room_id = mr.id " +
            "LEFT JOIN cancellation_requests cr ON r.id = cr.reservation_id " +
            "WHERE r.customer_id = #{customerId} " +
            "AND cr.id IS NULL " +
            "AND r.status IN ('CONFIRMED', 'LOCKED') " +
            "AND r.start_time > NOW() " +
            "ORDER BY r.start_time ASC")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "customer_id", property = "customerId"),
            @Result(column = "room_id", property = "roomId"),
            @Result(column = "customer_name", property = "customer.name"),
            @Result(column = "company_name", property = "customer.companyName"),
            @Result(column = "room_name", property = "room.name"),
            @Result(column = "room_type", property = "room.type")
    })
    List<Reservation> selectCancellableByCustomerId(@Param("customerId") Long customerId);
}